package com.koron.system.datadict.controller;

import com.koron.bean.base.Response;
import com.koron.common.core.business.datadict.bean.po.Lang;
import com.koron.common.core.business.datadict.bean.po.SysDataDict;
import com.koron.common.core.business.datadict.bean.po.SysDataDictDetail;
import com.koron.common.core.business.datadict.bean.query.SysDataDictQuery;
import com.koron.common.core.business.datadict.mapper.SysDataDictDetailMapper;
import com.koron.common.core.business.datadict.service.SysDataDictService;
import com.koron.common.core.util.FastJsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 系统管理-数据字典{@code SysDataDict}控制层
 *
 * <p>主要提供：增加、删除、修改、查询功能</p>
 *
 * @author 朱超
 * @since 2021-08-26
 */
@RestController
@RequestMapping("sys/DataDict")
@Api(tags = "系统管理-数据字典接口")
@Slf4j
public class SysDataDictController {
    private final SysDataDictService sysDataDictService;
    public SysDataDictController(SysDataDictService sysDataDictService) {
        this.sysDataDictService = sysDataDictService;
    }

    /**
     * 新增数据
     *
     * @param sysDataDict
     * @return Response对象
     * @see Response
     * @version 1.0
     */
    @PostMapping
    @ApiOperation(value = "新增单条数据字典")
    public Response insert(@RequestBody SysDataDict sysDataDict) {
        return ADOConnection.runTask(sysDataDictService, "checkBeforeInsert", Response.class, sysDataDict);
    }

    /**
     * 逻辑删除数据
     *
     * @param findId 主键
     * @return Response:删除结果
     * @see Response
     * @version 1.0
     */
    @DeleteMapping
    @ApiOperation(value = "逻辑删除单条数据字典", notes = "根据id逻辑删除单条数据")
    public Response delete(@RequestParam("findId") String findId) {
        return ADOConnection.runTask(sysDataDictService, "deleteById", Response.class, findId);
    }

    /**
     * 修改数据
     *
     * @param sysDataDict 实体对象
     * @return Response:更新成功的消息
     * @see Response
     * @version 1.0
     */
    @PutMapping
    @ApiOperation(value = "修改单条数据字典", notes = "id为必填项")
    public Response update(@RequestBody SysDataDict sysDataDict) {
        return ADOConnection.runTask(sysDataDictService, "checkBeforeUpdate", Response.class, sysDataDict);
    }


    /**
     * 根据条件查询出对应的数据字典数据(s_dictionary_type表)
     *
     * @param  sysDataDictQuery 请求对象
     * @return Response
     * @see Response
     * @version 1.0
     */
    @PostMapping("getDictionaryList")
    @ApiOperation(value = "分页获取对应的数据字典")
    public Response getDictionaryList(@RequestBody SysDataDictQuery sysDataDictQuery) {
        return ADOConnection.runTask(sysDataDictService,"getDictionaryList", Response.class, sysDataDictQuery);
    }

    /**
     * 根据传入的classifyId和code来返回对应的数据字典及其数据字典详情
     *
     * @param classifyId
     * @param code
     * @param factory
     * @return Response
     * @see Response
     * @version 1.0
     */
    @GetMapping("getByClassifyIdAndCode")
    @ApiOperation("根据传入的classifyCode和code来返回对应的数据")
    public Response getByClassifyIdAndCode(@RequestParam("classifyId") String classifyId, @RequestParam("code") String code, SessionFactory factory) {
        SysDataDict sysDataDict = sysDataDictService.getByClassifyIdAndCode(factory,classifyId,code);
        return Response.success(sysDataDict);
    }

    @GetMapping("/temp")
    @ApiOperation("更新多语种数据")
    public Response tempUpdata() {
        ADOConnection.runTask(sessionFactory -> {
            SysDataDictDetailMapper mapper = sessionFactory.getMapper(SysDataDictDetailMapper.class);
            List<SysDataDictDetail> all = mapper.getAll();

            for (SysDataDictDetail d : all) {
                Lang lang = new Lang();
                lang.setZhCn(d.getValue());
                lang.setZhHk(d.getZhHk());
                lang.setEn(d.getEn());
                String beanToJson = FastJsonUtils.getBeanToJson(lang);
                log.info(beanToJson);
                d.setLang(beanToJson);
            }
            mapper.updateBatch(all);
            return true;
        }, Boolean.class);
        return Response.success();
    }
}
